package kuaidi.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import kuaidi.helper.R;

/**
 * Created by Jhon on 2016/11/1.
 */

public class ChoseCompanyAdapter extends RecyclerView.Adapter<ChoseCompanyAdapter.ChoseCompanyViewHolder> implements View.OnClickListener {
    private List<String> data;
    private Context context;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public ChoseCompanyAdapter(List<String> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public ChoseCompanyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_chosecompany_layout, null);
        ChoseCompanyViewHolder choseCompanyViewHolder = new ChoseCompanyViewHolder(view);
        view.setOnClickListener(this);
        return choseCompanyViewHolder;
    }

    @Override
    public void onBindViewHolder(ChoseCompanyViewHolder holder, int position) {
        if (data.size() != 0) {
            holder.chosecompanyTextTv.setText(data.get(position));
            holder.itemView.setTag(data.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(view, (String) view.getTag());
        }
    }

    public class ChoseCompanyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.chosecompany_text_tv)
        TextView chosecompanyTextTv;

        public ChoseCompanyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, String data);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;

    }

}
